package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.annotation.Excel;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.ACheckInfor;
import com.ruoyi.system.domain.ATrainInfor;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.util.LoginUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.domain.AOperationRecords;
import com.ruoyi.system.service.IAOperationRecordsService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 下载收藏分享点赞记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-05-16
 */
@Service
@Transactional
public class AOperationRecordsServiceImpl implements IAOperationRecordsService 
{
    @Autowired
    private AOperationRecordsMapper aOperationRecordsMapper;

    @Autowired
    private AInformationMapper aInformationMapper;

    @Autowired
    private ADataInfoMapper aDataInfoMapper;

    @Autowired
    private ACheckInforMapper aCheckInforMapper;

    @Autowired
    private ATrainInforMapper aTrainInforMapper;
    /**
     * 查询下载收藏分享点赞记录
     * 
     * @param id 下载收藏分享点赞记录主键
     * @return 下载收藏分享点赞记录
     */
    @Override
    public AOperationRecords selectAOperationRecordsById(Long id)
    {
        return aOperationRecordsMapper.selectAOperationRecordsById(id);
    }

    /**
     * 查询下载收藏分享点赞记录列表
     * 
     * @param aOperationRecords 下载收藏分享点赞记录
     * @return 下载收藏分享点赞记录
     */
    @Override
    public List<AOperationRecords> selectAOperationRecordsList(AOperationRecords aOperationRecords)
    {
        return aOperationRecordsMapper.selectAOperationRecordsList(aOperationRecords);
    }

    /**
     * 新增下载收藏分享点赞记录
     * 
     * @param aOperationRecords 下载收藏分享点赞记录
     * @return 结果
     */
    @Override
    public int insertAOperationRecords(AOperationRecords aOperationRecords)
    {
        aOperationRecords.setCreateTime(DateUtils.getNowDate());
        return aOperationRecordsMapper.insertAOperationRecords(aOperationRecords);
    }

    /**
     * 修改下载收藏分享点赞记录
     * 
     * @param aOperationRecords 下载收藏分享点赞记录
     * @return 结果
     */
    @Override
    public int updateAOperationRecords(AOperationRecords aOperationRecords)
    {
        aOperationRecords.setUpdateTime(DateUtils.getNowDate());
        return aOperationRecordsMapper.updateAOperationRecords(aOperationRecords);
    }

    /**
     * 批量删除下载收藏分享点赞记录
     * 
     * @param ids 需要删除的下载收藏分享点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteAOperationRecordsByIds(Long[] ids)
    {
        return aOperationRecordsMapper.deleteAOperationRecordsByIds(ids);
    }

    /**
     * 删除下载收藏分享点赞记录信息
     * 
     * @param id 下载收藏分享点赞记录主键
     * @return 结果
     */
    @Override
    public int deleteAOperationRecordsById(Long id)
    {
        return aOperationRecordsMapper.deleteAOperationRecordsById(id);
    }

    @Override
    public int insertAOperationXcxRecords(AOperationRecords aOperationRecords) {
        int i = 0;
        aOperationRecords.setUserId(LoginUtils.getUser().getId());//需要动态获取userId
        List<AOperationRecords> list = aOperationRecordsMapper.selectAOperationRecordsList(aOperationRecords);
        OperTypeVO vo = new OperTypeVO();
        vo.setId(aOperationRecords.getDataId());
        vo.setOperType(aOperationRecords.getOperType());
        if(list.size() == 0){
            aOperationRecords.setIsShow(true);
            aOperationRecords.setNumSoft(0);
            aOperationRecords.setCreateTime(DateUtils.getNowDate());
            aOperationRecordsMapper.insertAOperationRecords(aOperationRecords);
            //根据对应资料的数量+1 数据分类，1首页数据。2素材库数据 //1首页数据使用a_information表  2素材库数据使用a_data_info表
            if("1".equals(aOperationRecords.getDataType())) {
                // 操作类型（1下载2收藏3分享4点赞）
                aInformationMapper.updateOper(vo);
            }else{
                aDataInfoMapper.updateOper(vo);
            }
            i = 1;
        }else{
            aOperationRecordsMapper.deleteAOperationRecordsXcxByrecords(aOperationRecords);
            //根据对应资料的数量-1
            if("1".equals(aOperationRecords.getDataType())) {
                // 操作类型（1下载2收藏3分享4点赞）
                aInformationMapper.updateMinusOper(vo);
            }else{
                aDataInfoMapper.updateMinusOper(vo);
            }
        }
        return i;
    }

    /***
     * 获取 分享数据列表
     * @param aOperationRecords
     * @return
     */
    @Override
    public List<XcxAInformationVO> queryInfoOperation(AOperationRecords aOperationRecords) {
        aOperationRecords.setUserId(LoginUtils.getUser().getId());
        List<XcxAInformationVO> list = aInformationMapper.queryInfoOperation(aOperationRecords);
        if(list.size() > 0){
            for (int i = 0 ; i <list.size() ;i++){
                String[] str =  list.get(i).getInfoImage().split(",");
                List<String> arr = new ArrayList<String>();
                for (String s : str){
                    arr.add(s);
                }
                list.get(i).setListImg(arr);
            }
        }
        return list;
    }

    @Override
    public List<XcxADataInfoVO> queryDataOperation(AOperationRecords aOperationRecords) {
        aOperationRecords.setUserId(LoginUtils.getUser().getId());
        List<XcxADataInfoVO> list = aDataInfoMapper.queryDataOperation(aOperationRecords);
        if(list.size() > 0){
            for (int i = 0 ; i <list.size() ;i++) {
                String[] str = list.get(i).getDaCarousel().split(",");
                List<String> arr = new ArrayList<String>();
                for (String s : str) {
                    arr.add(s);
                }
                list.get(i).setListImg(arr);
            }
        }
        return list;
    }

    @Override
    public List<XcxACheckInforVO> queryCheckOperation(AOperationRecords aOperationRecords) {
        aOperationRecords.setUserId(LoginUtils.getUser().getId());
        List<XcxACheckInforVO> list = aCheckInforMapper.queryCheckOperation(aOperationRecords);
        if(list.size() > 0){
            for (int i = 0 ; i <list.size() ;i++){
                String[] str =  list.get(i).getInfoImage().split(",");
                List<String> arr = new ArrayList<String>();
                for (String s : str){
                    arr.add(s);
                }
                list.get(i).setListImg(arr);
            }
        }
        return list;
    }

    @Override
    public List<XcxATrainInforVO> queryTrainOperation(AOperationRecords aOperationRecords) {
        aOperationRecords.setUserId(LoginUtils.getUser().getId());
        return aTrainInforMapper.queryTrainOperation(aOperationRecords);
    }
}
